<?php


namespace App\Repositories\Seller;


use App\Models\Finance;
use App\Repositories\BaseRepository;
use Illuminate\Database\Eloquent\Model;

class FinanceRepository extends BaseRepository
{

    /**
     * @return Model
     */
    public function model()
    {
        // TODO: Implement model() method.
        return new Finance();
    }
    public function sum($where=[],$type='flow'){
        return $this->model()->where($where)->when($type==='income',function ($query){
            return $query->where('type',array_keys(Finance::$mapIncomeType));
        })->when($type==='expend',function ($query){
            return $query->whereIn('type',array_keys(Finance::$mapExpendType));
        })->sum('amount');

    }
    public static function add($seller_id, $money, $desc = '充值', $type = 1,$generated_time='')
    {

        $insertData = [];
        $insertData['seller_id'] = $seller_id;
        $insertData['amount'] = abs($money);
        $insertData['desc'] = (string)$desc;
        $insertData['type'] = $type;
        $insertData['is_income'] = key_exists($type,Finance::$mapIncomeType);
        $insertData['generated_at'] = $generated_time?:now();
        return (new static())->model()->create($insertData);
    }
}